<template>
  <div class="action-form">
      <FormControl module="form"></FormControl>
  </div>
</template>

<script>
import $ from "@/common";
import base from "../base";
import FormControl from "@/x_components/form/index";
import { billParser } from "@/common/parser";

export default {
  subModuleStore: true,
  extends: base,
  data() {
    return {
      path: {
        submit: "/service/{app}/bill/data/{bill_code}",
        find: "/service/{app}/bill/data/{bill_code}?id={id}",
        model: "/service/{app}/bill/model/{bill_code}",
        def: "/service/{app}/bill/model/{bill_code}/def_value"
      },
      modelLoad: false,
      dataLoad: false
    };
  },
  computed: {
    formData() {
      return this.$store.getters[`${this.$pathCode}/form/formData`];
    },
    formValidate() {
      if (!this.moduleState.form) {
        return true;
      } else {
        return this.moduleState.form.validate;
      }
    },
    form() {
      return this.moduleState.form;
    },
    //获得模型链接
    modelPath() {
      if (this.actionParam.bill_code || this.actionParam.path_model) {
        return $.composePath(this.actionParam.path_model || this.path.model, {
          bill_code: this.actionParam.bill_code
        });
      }
    },
    //获得取数链接
    dataPath() {
      return $.composePath(this.actionParam.path_data || this.path.find, {
        bill_code: this.actionParam.bill_code,
        ...this.triggerData.data
      });
    },
    local() {
      return this.model.param && this.model.param.local;
    }
  },
  methods: {
    onTrigger() {
      //this.actionActive();
      this.actionFetch();
      //为读取model则先读取model，读取过的就先获取数据
      if (!this.modelLoad) {
        this.fetchModel()
          .then(({ data }) => {
            this.modelLoad = true;
            this.setModel(data);
          })
          .catch(ex => {
            console.warn(ex);
            this.actionError("ready");
          });
      } else {
        this.formClear();
        this.fetchData()
          .then(res => {
            this.dataLoad = true;
            this.setData(res.data);
          })
          .catch(ex => {
            console.warn(ex);
            this.actionError("ready");
          });
      }
    },
    onSubmit() {
      this.$dispatch("form/doValidate");
      if (!this.moduleState.form.validate) {
        return this.pushStatus("active");
      } else {
        return this.formSubmit()
          .then(() => {
            this.actionSuccess("数据保存成功");
          })
          .catch(() => {
            this.actionError("ready");
          });
      }
    },
    formSubmit() {
      return this.formPut();
    },
    formPut() {
      return this.submit(this.path.submit, this.formData, {
        method: "put"
      });
    },
    fetchModel() {
      if (this.modelPath) {
        return this.fetch(this.modelPath);
      } else {
        this.modelLoad = true;
        return new Promise(resolve => {
          resolve({ data: this.$owner.model });
        });
      }
    },
    formFetch() {
      return this.fetch(this.dataPath);
    },
    fetchData() {
      if (this.dataPath) {
        return this.formFetch();
      } else {
        return new Promise(resolve => {
          resolve({ data: {} });
        });
      }
    },
    //设置数据
    setData(data) {
      this.setFormData(data);
      this.actionActive();
    },
    //设置模型
    setModel(data) {
      this.setFormModel(data);
      this.actionActive();
    },
    //model解析
    parseModel(model) {
      if (model.isParse) {
        return model;
      } else {
        return billParser(model);
      }
    },
    //form辅助调用方法
    setFormData(data) {
      this.$commit("form/setData", data);
    },
    setFormModel(data) {
      this.$dispatch("form/updateModel", this.parseModel(data));
    },
    formClear(data) {
      this.$commit("form/resetFields");
    }
  },
  components: {
    FormControl
  }
};
</script>

<style>

</style>
